import urllib.request
import urllib.parse
import json

# get请求   获取豆瓣电影第一页
url='https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20'

headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
}
# 请求对象的定制
request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
content =response.read().decode('utf-8')
print(content)


# 数据下载到本地
# open方法默认使用GBK 需要指定为utf8   encoding='utf-8'

# 写入1
fp = open('db.json','w',encoding='utf-8')
fp.write(content)
# 写入2
# with open('db.json','r',encoding='utf-8') as f:
#     fp.write(content)